package com.cg.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.cg.common.Constants;
import com.cg.config.MyAlipayConfig;
import com.cg.dto.Dto;
import com.cg.dto.DtoUtil;
import com.cg.dto.OrderManagerDto;
import com.cg.service.DmOrderService;
import com.cg.vo.OrderManagerVo;
import com.cg.vo.QueryOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 订单表(DmOrder)表控制层
 *
 * @author makejava
 * @since 2023-11-13 09:49:23
 */
@RestController
@RequestMapping("order")
@Slf4j
public class DmOrderController {
    /**
     * 服务对象
     */
    @Resource
    private DmOrderService dmOrderService;


    @Resource
    private MyAlipayConfig myAlipayConfig;


    //根据订单状态、订单创建时间、关键词(商品名称，订单号，商品编号模糊)
    @PostMapping("/queryOrderList")
    public Dto<List<OrderManagerVo>> queryOrderList(@RequestBody OrderManagerDto orderManagerDto){
        return DtoUtil.returnDataSuccess(dmOrderService.queryOrderList(orderManagerDto));
    }


    //根据订单号查询订单
    @PostMapping("/queryOrderByOrderNo")
    public Dto<QueryOrderVo> queryOrderByOrderNo(@RequestParam("orderNo") String orderNo){
        return DtoUtil.returnDataSuccess(dmOrderService.queryOrderByOrderNo(orderNo));
    }


    //订单支付
    @GetMapping("/toPay")
    public String toPay(@RequestParam("orderNo") String orderNo) throws AlipayApiException {
        return dmOrderService.toPay(orderNo);
    }


    //同步通知
    @PostMapping("/returnUrl")
    public String returnUrl(@RequestBody Map<String,String[]> requestParams) throws UnsupportedEncodingException, AlipayApiException {
        log.info("provider接收到同步通知");
        //解析参数
        //获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        //验签
        boolean verify_result = AlipaySignature.rsaCheckV1(params, myAlipayConfig.getAlipayPublicKey(), myAlipayConfig.getCharset(), myAlipayConfig.getSignType());

        if(!verify_result){
            log.info("同步校验验证失败");
            return Constants.AliPayStatus.SIGN_FAIL;
        }
        log.info("同步校验验证成功");
        //业务处理
        return Constants.AliPayStatus.SUCCESS_INFO;
    }
    //异步通知
    @PostMapping("/aliNotify")
    public String notifyUrl(@RequestBody Map<String,String[]> requestParams) throws AlipayApiException {
        log.info("provider接收到异步通知");
        //解析参数
        Map<String,String> params = new HashMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }
        //验签
        boolean verify_result = AlipaySignature.rsaCheckV1(params, myAlipayConfig.getAlipayPublicKey(), myAlipayConfig.getCharset(), myAlipayConfig.getSignType());
        if(!verify_result){
            log.info("异步通知校验失败");
            return Constants.AliPayStatus.FAIL;
        }
        log.info("异步通知校验成功");
        //业务处理
        return dmOrderService.notify(params);
    }

}

